package com.cy;

import com.alibaba.csp.sentinel.adapter.servlet.CommonFilter;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @author: Luckyh
 * @date : 2021/4/8 16:26
 */
@SpringBootApplication
@EnableFeignClients  // 启动feign的调用
public class NacosConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(NacosConsumerApplication.class, args);
    }



    // 初始化restTemplate 可以通过http协议调用远程服务
    @Bean
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }

//    @Bean
//    public FilterRegistrationBean sentinelFilterRegistration() {
//        FilterRegistrationBean registration = new FilterRegistrationBean();
//        registration.setFilter(new CommonFilter());
//        registration.addUrlPatterns("/*");
//        // 入口资源关闭聚合
//        registration.addInitParameter(CommonFilter.WEB_CONTEXT_UNIFY, "false");
//        registration.setName("sentinelFilter");
//        registration.setOrder(1);
//        return registration;
//    }

    @Bean
    @LoadBalanced
    public RestTemplate loadRestTemplate(){
        return new RestTemplate();
    }

    @Value("${spring.application.name}")
    private String applicationName;

    @Autowired
    private RestTemplate loadRestTemplate;
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private LoadBalancerClient loadBalancerClient;


    @Service
    public class Messageml {
        @SentinelResource("message")
        public String message(){
            return "message";
        }
    }

    @RestController
    public class ConsumerController {

        @Autowired
        private Messageml messageml;

        @GetMapping("/consumer/doFindById")
        @SentinelResource("doFindById") // 需要限流的话，需要加注解
        public String doFindById(@RequestParam(value = "id",required = false) Integer id,
                                 @RequestParam(value = "name",required = false) String name){
            return "host is id:"+id+"name:"+name;
        }

        @GetMapping("/consumer/doRestTemplate/")
        public String doRestTemplate(){
            messageml.message();
            //doGetResource();
            String url = String.format("http://%s/provider/echo/%s", "nacos-provider", applicationName);
            return loadRestTemplate.getForObject(url, String.class);
        }

        @GetMapping("/consumer/doRestEcho")
        public String doRestEcho() {
            //doGetResource();
            ServiceInstance serviceInstance =
                    loadBalancerClient.choose("nacos-provider");
            String host = serviceInstance.getHost();
            int port = serviceInstance.getPort();

            String url = String.format("http://%s:%s/provider/echo/%s", host, port, applicationName);
            System.out.println(url);

            //String url = "http://"+host+":"+port+"/provider/echo/"+applicationName;
            //String url = "http://localhost:8081/provider/echo/"+applicationName;
            //System.out.println(url);
            //return url;
            return restTemplate.getForObject(url,String.class);
            //return "consumer echo:"+applicationName;
        }

    }
}
